import axios, { type AxiosResponse } from 'axios'

const request = axios.create({
  baseURL: '/api',
  // baseURL: 'http://localhost:8080',
  timeout: 10000,
  withCredentials: true, //携带cookie
})

request.interceptors.request.use(
  function (config) {
    return config
  },
  function (error) {
    return Promise.reject(error)
  },
)

request.interceptors.response.use(
  function (response: AxiosResponse) {
    const { data } = response
    // 未登录
    // if (data.message === '未登录') {
    //   // 不是登录页面，则跳转到登录页面
    //   if (
    //     !response.request.responseURL.includes('user/current') &&
    //     !window.location.pathname.includes('/login')
    //   ) {
    //     window.location.href = `login?redirect=${window.location.href}`
    //   }
    // }
    return data
  },
  function (error) {
    return Promise.reject(error)
  },
)

export default request
